package servlet;

import beans.SessionScopeBean;
import hibernate.HibernateOperations;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


/**
 * Created with IntelliJ IDEA.
 * User: shprotik
 * Date: 05.04.12
 * Time: 0:27
 * To change this template use File | Settings | File Templates.
 */
public class SaveServlet extends HttpServlet {

    public void init(ServletConfig config) throws ServletException {
        super.init(config);

    }

    protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
        List<SessionScopeBean> list = new ArrayList<SessionScopeBean>();


        HibernateOperations obj = new HibernateOperations();
        obj.insert(req.getParameter("fname"), req.getParameter("sname"), req.getParameter("lname"));
        List<hibernate.Bean> inf = obj.getAll();
        for (int i = 0; i < inf.size(); i++) {
            SessionScopeBean value = new SessionScopeBean();
            value.setName(inf.get(i).getFirstName());
            value.setSecondName(inf.get(i).getSecondName());
            value.setSurname(inf.get(i).getLastName());
            list.add(value);

        }
        req.getSession().setAttribute("collection", inf);

    }
    protected void doPost(HttpServletRequest req,HttpServletResponse res) throws IOException {
        String url = "show.jsp";
        res.sendRedirect(res.encodeRedirectURL(url));
    }
}

